package com.xzgj.fmmall.webSocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * webSocket消息推送
 */
@Component
@ServerEndpoint("/webSocket/{orderId}")
public class WebSocketServer {

    private static ConcurrentHashMap<String,Session> sessionMap = new ConcurrentHashMap<>();
    /**
     * 前端发送请求建立websocket连接，就会执行OnOpen
     */
    @OnOpen
    public void open(@PathParam("orderId") String orderId, Session session){
        System.out.println("---------:建立连接:"+orderId);
        sessionMap.put(orderId,session);
    }

    /**
     * 前端关闭页面或者主动关闭websocket连接，就会执行OnClose
     */
    @OnClose
    public void close(@PathParam("orderId") String orderId){

        sessionMap.remove(orderId);
    }

    public static void sendMsg(String orderId,String msg){

        try {
            Session session = sessionMap.get(orderId);
            session.getBasicRemote().sendText(msg);
        }catch (Exception e){

            e.printStackTrace();
        }

    }
}
